package com.develop.BeanUtils;

import java.util.List;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import com.develop.service.AlarmConsumer;

@Component
public class KafkaConsumer {
	private static final Logger logger = LoggerFactory.getLogger(KafkaConsumer.class);
	@Autowired
	private AlarmConsumer alarmConsumer;

	/**
	 * 监听topic5,批量消费
	 */
	@KafkaListener(topics = "${topicName}", containerFactory = "batchFactory")
	public void listen2(List<ConsumerRecord<String, String>> records) {
		alarmConsumer.saveAlarm(records);
	}

	/**
	 * 批量消费
	 */
	public void batchConsumer(List<ConsumerRecord<String, String>> records) {
		records.forEach(record -> consumer(record));
	}

	/**
	 * 单条消费
	 */
	public void consumer(ConsumerRecord<String, String> record) {
		logger.debug("主题:{}, 内容: {}", record.topic(), record.value());
	}
}
